package com.bnzj.cloud.business.core.service;

import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.bnzj.cloud.business.core.persistence.entity.SysDepartment;
import com.bnzj.cloud.business.core.persistence.repository.SysDepartmentRepository;
import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.query.QueryHelpBean;

@Service
public class SysDepartmentService extends BaseService<SysDepartmentRepository, SysDepartment, Long> {

	public List<SysDepartment> findAllByEnabled() {
		return repository.findAllByEnabled(true, Sort.by("pid", "sequence"));
	}

	public static QueryHelpBean getQueryHelpBean() {
		String selectSQL = "select d.*";
		String whereSQL = " from bnz_sys_department d where 1=1 ";
		QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, SysDepartment.class);
		return queryBean;
	}

	@Transactional(rollbackOn = Exception.class)
	public int disable(List<Long> id) {
		return repository.disable(id);
	}

	public Set<Long> findAllIdByEnabled() {
		return repository.findAllIdByEnabled();
	}

	/**
	 * @param id
	 */
	@Transactional(rollbackOn = Exception.class)
	public void insertId(Long id) {
		repository.insertId(id);
	}

	/**
	 * 递归
	 */
	public static QueryHelpBean findQueryHelpBean() {
		String selectSQL = " select d.*  ";
		String whereSQL = " from bnz_sys_department d, bnz_sys_department e "
				+ "where d.full_name like concat(e.full_name, \"%\") ";
		QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, SysDepartment.class);
		return queryBean;
	}

	public List<SysDepartment> findChild(Long id) {
		return repository.findAllByEnabledAndPid(true, id);
	}
	
	public String findPrimaryDept(String deptId) {
		if(null!=deptId&&!"".equals(deptId)) {
			SysDepartment dept = repository.findById(Long.parseLong(deptId)).get();
			if(null!=dept) {
				if("0".equals(dept.getPid().toString())) {
					return dept.getId().toString();
				} else {
					SysDepartment parentDept = repository.findById(dept.getPid()).get();
					if("0".equals(parentDept.getPid().toString())) {
						return dept.getId().toString();
					}else {
						return this.findPrimaryDept(parentDept.getId().toString());
					}
				}
			}
		}
		return null;
	}
	
	public String findSecondaryDept(String deptId) {
		if(null!=deptId&&!"".equals(deptId)) {
			SysDepartment dept = repository.findById(Long.parseLong(deptId)).get();
			if(null!=dept) {
				if(!"0".equals(dept.getPid().toString())) {
					SysDepartment parentDept = repository.findById(dept.getPid()).get();
					if("0".equals(parentDept.getPid().toString())) {
						return dept.getPid().toString();
					}else {
						SysDepartment parentParentDept = repository.findById(parentDept.getPid()).get();
						if("0".equals(parentParentDept.getPid().toString())) {
							return deptId;
						}else {
							return this.findSecondaryDept(parentDept.getId().toString());
						}
					}
				}
			}
		}
		return null;
	}
}
